from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
# Create your tests here.

from .models import Funkspruch

class FunkspruchModelTests(TestCase):
    #example:
    """
    def function_name_with_breaking_thing(self):
        \"\"\"
        function_name returns whatever for thing with breaking
        \"\"\"
        create_breaking = input_
        with_that_funkspruch = Funkspruch(thing=create_breaking)
        self.assertIs(with_that_funkspruch.function_name, expectedResult)
    #then python manage.py test funk
    """

class FunkIndexViewTests(TestCase):
    def testTodayFilterAt0AM(self):
        date = timezone.localdate() # create date which contains todays local date
        timeZone = timezone.tzinfo("Europe/Berlin") # create a timezone object
        time = timezone.datetime(date.year, date.month, date.day, 0,0,0,0) # set the time to today 0 AM
        create_funk(funk_message="0AM",funk_date=time)
        response = self.client.get(reverse('funk:index'))
        self.assertQuerysetEqual(
            response.context['todays_funks'],
            ['<Funkspruch: 0AM>']
        )

    def testTodayFilterAtAlmostNextDay(self):
        date = timezone.localdate() # create date which contains todays local date
        timeZone = timezone.tzinfo("Europe/Berlin") # create a timezone object
        time = timezone.datetime(date.year, date.month, date.day, 23,59,59,1000000-1) # set the time to today 23:59:59
        create_funk(funk_message="23H59Min59s",funk_date=time)
        response = self.client.get(reverse('funk:index'))
        self.assertQuerysetEqual(
            response.context['todays_funks'],
            ['<Funkspruch: 23H59Min59s>']
        )

    def testTodayFilterAtOneSecondBeforeThisDay(self):
        date = timezone.localdate() # create date which contains todays local date
        timeZone = timezone.tzinfo("Europe/Berlin") # create a timezone object
        time = timezone.datetime(date.year, date.month, date.day-1, 23,59,59,1000000-1) # set the time to yesterday 23:59:59
        create_funk(funk_date=time)
        response = self.client.get(reverse('funk:index'))
        self.assertContains(response, "Heute wurden noch keine Funkrufe abgegeben.")
        
        
    def testTodayFilterAtTomorrow(self):
        date = timezone.localdate() # create date which contains todays local date
        timeZone = timezone.tzinfo("Europe/Berlin") # create a timezone object
        time = timezone.datetime(date.year, date.month, date.day+1, 0,0,0,0) # set the time to yesterday 23:59:59
        create_funk(funk_date=time)
        response = self.client.get(reverse('funk:index'))
        self.assertContains(response, "Heute wurden noch keine Funkrufe abgegeben.")
        
        


def create_funk(funk_message="Test", funk_from="test1", funk_to="test2", funk_date=timezone.localtime()):
    """
    Creates a Funkspruch with given data.
    """
    return Funkspruch.objects.create(message=funk_message, fromOp=funk_from, toOp=funk_to, date=funk_date)

